/*
* $Id: frame.h,v 1.21 2008/01/16 06:55:35 chunchia Exp $
*/
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: frame.h

       Version: 0.3.0

       Usage: Define MAC frame

       Platform: U-NET01 DK with Keil 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124, C8051F340

               UBEC: UZ2400, UZ2410

       Note :

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/

//IEEE 802.15.4 frame type
#define FRM_TYPE_BCN			0
#define FRM_TYPE_DATA			1
#define FRM_TYPE_ACK			2
#define FRM_TYPE_COMM			3

//IEEE 802.15.4 command type
#define MT_ASSO_REQ			0x01
#define MT_ASSO_RSP			0x02
#define MT_DISASSO_NTF			0x03
//#define MT_DATA_REQ			0x04
//#define MT_PANID_CONFLICT		0x05
//#define MT_ORPHAN_NTF			0x06
#define MT_BEACON_REQ			0x07
//#define MT_COOR_REALIGNMENT		0x08
//#define MT_GTS_REQ			0x09

#define MT_DATA_IND			0x0a
#define MT_BCN_IND			0x0b
#define MT_IPOP				0x0c
#define MT_PBACK			0x0d
#define MT_CBACK			0x0e
#define MT_REBPAN			0xff

//-------------------------------------------------------------------------------

//Define the UNET frame type: seen on RF communication among nodes
#define UNET_DATA			0x00
#define UNET_RESET			0x01
#define UNET_LEAVE_CMD			0x02
#define UNET_KICK_CMD			0x03
#define UNET_JOIN_REPORT		0x04
#define UNET_QUERY_CMD			0x05
#define UNET_ASK_MAC_ADDR		0x06
#define UNET_CS				0x07
#define UNET_LL				0x08
#define UNET_RSP_MAC_ADDR		0x09
#define UNET_LEAVE_REPORT		0x0a
#define UNET_RSP_QUERY			0x0b
#define	UNET_MAC_ROUTE			0x0c
#define UNET_QUERY_RELAY		0x0d
#define UNET_LOCAL_QUERY		0x0e
#define	UNET_LOCAL_QUERY_RSP		0x0f
#define UNET_LOCAL_TALK			0x10
#define UNET_LOCAL_TALK_RSP		0x11
#define UNET_PERMIT_JOIN_R		0x12
#define UNET_RX_DATA			0x13
#define UNET_REQ_TAB			0x14
#define UNET_RSP_TAB			0x15

#define UNET_REQ_ROUTERTAB		0xf0
#define UNET_RSP_ROUTERTAB 		0xf1
#define UNET_REQ_DEVTAB			0xf2
#define UNET_RSP_DEVTAB			0xf3
#define UNET_GET_INFO			0xf4
#define UNET_PERMIT_JOIN		0xf5

//Define the internal debug command
#define UNET_DBG_RSSI_QUERY		0x70

#define UNET_DBG_NODE_INFO_REQ	0x71
#define UNET_DBG_NODE_INFO_RSP	0x72

#define UNET_DBG_CHILD_MONITOR	0x73

/********************************************************************************

	MAC Packet Format

********************************************************************************/

#if (DATA_BUS_WIDTH >= 16)		// This is a 16-bit (or above) CPU
#pragma pack(1)
#endif // #if (DATA_BUS_WIDTH >= 16)

// Frame Control of IEEE 802.15.4
typedef union _mac_frame_control_{
	UINT16 val;

	struct{
		UINT16 FrmType:3;
		UINT16 Security:1;
		UINT16 FrmPend:1;
		UINT16 AckReq:1;
		UINT16 IntraPAN:1;
		UINT16 Rvd0:3;
		UINT16 DstMod:2;
		UINT16 Rvd1:2;
		UINT16 SrcMod:2;
	}bits;
}MAC_FC;

typedef struct _mac_header_{
	MAC_FC Header;
	UINT8 Seqnum; //Sequence number
	UINT16 DstPID; // Destination pan id
	UINT16 DstAddr; // Destination address
	UINT16 SrcPID;// Source pan id
	UINT16 SrcAddr; // Source address
}MAC_HEADER;

typedef struct _mac_beacon_payload_{
	UINT8 ProtID;		// Protocol ID
	UINT8 StackPro:4;	// Stack profile
	UINT8 ProtVer:4;	// Protocol version
	UINT8 Rvd0:2;
	UINT8 RouterCap:1;	// Router capacity
	UINT8 DevDep:4;		// Device depth
	UINT8 EndevCap:1;	// End device capacity
	UINT8 ChildCnt;		// Children count
	UINT8 Channel;		// Channel number
}MAC_BP;

typedef struct _mac_beacon_packet_{
	MAC_FC Header;
	UINT8 Seqnum;		// Sequence number
	UINT16 SrcPID;		// Source pan id
	UINT16 SrcAddr;		// Source address
	UINT16 SFS;		// Superframe specification
	UINT8 GTSF;		// GTS Filed
	UINT8 PASF;		// Pending address specification filed
	MAC_BP PAYLOAD;		// Beacon payload
	//UINT16 FCS
}MAC_BCN_PKT;

//typedef struct _mac_data_packet_{
//	MAC_HEADER MHR;
//	UINT8 Radius;
//	UINT16 NwkSrcAddr; // Unet spec
//	UINT16 NwkDstAddr; // Unet spec
//	//UINT8 *PAYLOAD;
//}MAC_DATA_PKT;

typedef struct _mac_command_packet_{
	MAC_HEADER MHR;
	UINT8 COMM_ID;
	//UINT8 *PAYLOAD;
	//UINT16 FCS
}MAC_CMD_PKT;



//-------------------------------------------------------------------------------

typedef struct _unet_header_info_
{
	MAC_HEADER MHR;
	UINT8 UnetFrmLen;
	UINT8 Radius;
	UINT8 UnetSeqNum;
	UINT8 UnetFrmType;
	UINT16 DstAddr;
	UINT16 SrcAddr;
}UNET_HEADER;

typedef struct _unet_kick_frame_
{
	UNET_HEADER UNETHeader;
	UINT16 KickChildAddress;
	UINT16 NewParent;
}UNET_KICK_FRAME;

typedef struct _unet_leave_frame_
{
	UNET_HEADER UNETHeader;
	UINT16 LeaveChildAddress;
}UNET_LEAVE_FRAME;

typedef struct _unet_join_report_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 JoinChildMACAddress[8];
	UINT16 JoinChildNWKAddress;
}UNET_JOINREPORT_FRAME;

typedef struct _unet_rsp_ma_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 RspMACAddress[8];
}UNET_RSP_MA_FRAME;

typedef struct _unet_mac_route_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 DstMACAddress[8];
	UINT8 DataLength;
}UNET_MAC_ROUTE_FRAME;

typedef struct _unet_query_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 RXRSSI;
	UINT8 TXRSSI;
}UNET_QUERY_FRAME;

typedef struct _unet_local_query_frame_
{
	UNET_HEADER UNETHeader;
	UINT32 Interval;
	UINT8 Switch;
}UNET_LOCAL_QUERY_FRAME;

typedef struct _unet_reset_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 maxchild;
	UINT8 maxrouter;
	UINT8 maxdepth;
}UNET_RESET_FRAME;

typedef struct _child_table_
{
	UINT8 mac_address[8];
	UINT16 nwk_address;
}CHILD_TABLE;

typedef struct _unet_tab_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 chi_cnt;
	CHILD_TABLE chi_tab[8];
}UNET_TAB_FRAME;

typedef struct _unet_node_info_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 mac_address[8];
	UINT16 parent_nwk_address;
	UINT16 my_nwk_address;
}UNET_NODE_INFO_FRAME;

typedef struct _unet_local_talk_frame_
{
	UNET_HEADER UNETHeader;
	UINT8 mac_address[8];
	UINT32 poll_interval;
}UNET_LOCAL_TALK_FRAME;


//-------------------------------------------------------------------------------

/********************************************************************************

	MAC Sub Command Packet Format

********************************************************************************/

// Association request
union _capability_information_field_{
	UINT8 val;

	struct{
		UINT8 AltCoord:1; // Alternate PAN coordinator
		UINT8 DevType:1; // Device type
		UINT8 PowSrc:1; // Power source
		UINT8 ROWI:1; // Receiver on when idle
		UINT8 Rvd0:2;
		UINT8 SecCap:1; // Security capability
		UINT8 AllocAddr:1; // Allocate address
	}bits;
};

typedef struct _association_request_{
	MAC_FC Header;
	UINT8 Seqnum; //Sequence number
	UINT16 DstPID; // Destination pan id
	UINT16 DstAddr; // Destination address
	UINT16 SrcPID;// Source pan id
	UINT8 SrcAddr[8]; // Source address
	UINT8 COMM_ID;
	//union _capability_information_field_ Cap;
	UINT8 Cap;
	//UINT16 FCS
}MCP_ASSO_REQ; // MAC Command Packet


//Association Status
#define ASSOCIATION_SUCCESS	0x00
#define PAN_AT_CAPACITY		0x01
#define PAN_ACCESS_DENIED	0x02
#define MEMORY_FULL  		0x03 // Specific status
#define OTHER_STATUS		0x04

typedef struct _association_reponse_{
	//MAC_CMD_PKT CmdFrm;
	MAC_FC Header;
	UINT8 Seqnum;		// Sequence number
	UINT16 DstPID;		// Destination pan id
	UINT8 DstAddr[8];	// Destination address
	UINT16 SrcPID;		// Source pan id
	UINT16 SrcAddr;		// Source address
	UINT8 COMM_ID;		// Command identifiter
	UINT16 ShortAddr;	// Short address
	UINT8 AssoStatus;	// Association status
	UINT8 MaxChild;
	UINT8 MaxRouter;
	UINT8 MaxDepth;
	//UINT16 FCS
}MCP_ASSO_RSP;

typedef struct _bacon_request_{
	MAC_FC Header;
	UINT8 Seqnum; //Sequence number
	UINT16 DstPID; // Destination pan id
	UINT16 DstAddr; // Destination address
	UINT8 COMM_ID;
}MCP_BCN_REQ;

typedef struct _disassociation_notification_{
	MAC_FC Header;
	UINT8 Seqnum; //Sequence number
	UINT16 DstPID; // Destination pan id
	UINT8 DstAddr[8]; // Destination address
	UINT16 SrcPID;// Source pan id
	UINT16 SrcAddr; // Source address
	UINT8 COMM_ID; // Command identifiter
	UINT8 DisassoReason; // Short address
	UINT16 NewParent;
}MCP_DISASSO_NTF;

#if (DATA_BUS_WIDTH >= 16)		// This is a 16-bit (or above) CPU
#pragma pack()
#endif // #if (DATA_BUS_WIDTH >= 16)
